package com.unionfetch.content_home.ui

import com.unionfetch.content_home.bean.UserStatus
import com.unionfetch.content_home.bean.UserType
import com.unionfetch.content_home.bean.findUserStatus
import com.unionfetch.content_home.ui.auth.AddressAuthActivity
import com.unionfetch.content_home.ui.auth.IdentityAuthActivity
import com.unionfetch.content_home.ui.auth.SumsubManager
import com.unionfetch.content_home.ui.step.FirstStepOpenAccountActivity
import com.unionfetch.content_home.ui.step.SecondStepOpenAccountActivity
import com.unionfetch.content_home.ui.step.ThirdStepOpenAccountActivity
import com.unionftech.common.common.GlobalInfoManager
import com.unionftech.common.utils.ActivityCollector
import com.unionftech.common.utils.LogUtils

object OpenAccountGuideUtil {


    /**
     * 跳转开户的每一步
     */
    @JvmStatic
    fun gotoOpenAccountStep() {
        GlobalInfoManager.getInstance().getSystemUserInfo()?.also { info ->
            ActivityCollector.getCurrentActivity()?.also { activity ->
                val isIb = info.type == UserType.IB.type
                if (isIb) {
                    SecondStepOpenAccountActivity.startThis(activity, true)
                } else {
                    when (info.status.findUserStatus()) {
                        UserStatus.TRADER_OPEN_ACCOUNT -> {
                            FirstStepOpenAccountActivity.startThis(activity)
                        }

                        UserStatus.COUNTRY_SETTING -> {
                            SecondStepOpenAccountActivity.startThis(activity)
                        }

                        UserStatus.TRADER_ACCOUNT_SETTING -> {
                            ThirdStepOpenAccountActivity.startThis(activity)
                        }

                        else -> {}
                    }
                }
            }
        }
    }

    /**
     * 跳转身份认证
     */
    @JvmStatic
    fun gotoPoiAuth(isSupplement: Boolean, isContinuePoa: Boolean) {
        val userInfo = GlobalInfoManager.getInstance().getSystemUserInfo()
        userInfo?.also { info ->
            if (info.thirdPartySwitch && !info.kycCountrySetting.contains(info.residence)) {
                LogUtils.d("OpenAccountGuideUtil", "gotoPoiAuth start sumsub")
                //开启sumsub 身份认证
                ActivityCollector.getCurrentActivity()?.also { activity ->
                    val userToken = GlobalInfoManager.getInstance().getUserToken()
                    GlobalInfoManager.getInstance().getSystemUserInfo()
                    SumsubManager.getInstance().startSumsubPoi(
                        activity,
                        userToken,
                        info.userId.toString(),
                        isSupplement = isSupplement,
                        isContinuePoa = isContinuePoa
                    )
                }
            } else {
                LogUtils.d("OpenAccountGuideUtil", "gotoPoiAuth start upload")
                ActivityCollector.getCurrentActivity()?.also { activity ->
                    IdentityAuthActivity.startThis(
                        activity, isSupplement = isSupplement, isContinuePoa = isContinuePoa
                    )
                    activity.finish()
                }
            }
        }
    }

    /**
     * 跳转地址认证
     */
    @JvmStatic
    fun gotoPoaAuth(isSupplement: Boolean) {
        val userInfo = GlobalInfoManager.getInstance().getSystemUserInfo()
        userInfo?.also { info ->
            if (info.thirdPartySwitch && !info.kycCountrySetting.contains(info.residence)) {
                //开启sumsub 地址认证
                LogUtils.d("OpenAccountStateView", "gotoPoaAuth start sumsub")
                ActivityCollector.getCurrentActivity()?.also { activity ->
                    val userToken = GlobalInfoManager.getInstance().getUserToken()
                    GlobalInfoManager.getInstance().getSystemUserInfo()
                    SumsubManager.getInstance().startSumsubPoa(
                        activity,
                        userToken,
                        info.userId.toString(),
                        isSupplement
                    )
                }
            } else {
                ActivityCollector.getCurrentActivity()?.also { activity ->
                    AddressAuthActivity.startThis(activity, isSupplement)
                    activity.finish()
                }
            }
        }
    }
}